package com.video.controller.webcon;

import com.qf.entity.ResultData;
import com.video.commons.loginaop.LoginUser;
import com.video.commons.loginaop.MustLogin;
import com.video.commons.sendaop.SocketSend;
import com.video.entity.Solution;
import com.video.entity.Webcon;
import com.video.netty.send.DelSolutionTaskSend;
import com.video.netty.send.ModifySolutionTaskSend;
import com.video.netty.send.StartSolutionTaskSend;
import com.video.service.webcon.ISolutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/solution")
public class SolutionController {

    @Autowired
    private ISolutionService solutionService;

    /**
     * 获得方案列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    @MustLogin
    public ResultData<List<Solution>> queryList(@LoginUser Webcon webcon){
        List<Solution> solutions = solutionService.queryByAccount(webcon.getAccount());
        return new ResultData<List<Solution>>().setData(solutions);
    }

    /**
     * 添加方案
     * @param solution
     * @param webcon
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    @MustLogin
    public ResultData<Integer> insertSolut(Solution solution, @LoginUser Webcon webcon){
        solution.setAccount(webcon.getAccount());
        Integer result = solutionService.insertSolution(solution);
        return new ResultData<Integer>().setData(result);
    }

    /**
     * 修改方案
     * @param solution
     * @param webcon
     * @param oldName
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    @MustLogin
    @SocketSend(params = {"#oldName", "#solution", "#result"}, sendClass = ModifySolutionTaskSend.class)
    public ResultData<Integer> updateSolut(Solution solution, @LoginUser Webcon webcon, String oldName){
        solution.setAccount(webcon.getAccount());
        int result = solutionService.updateSolution(solution, oldName);
        return new ResultData<Integer>().setData(result);
    }

    /**
     * 删除方案
     * @param solution
     * @param webcon
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @MustLogin
    @SocketSend(params = {"#solution"}, sendClass = DelSolutionTaskSend.class)
    public ResultData<Integer> deleteSolut(Solution solution,
                                           @LoginUser Webcon webcon){
        solution.setAccount(webcon.getAccount());
        int result = solutionService.deleteSolution(solution);
        return new ResultData<Integer>().setData(result);
    }

    /**
     * 执行方案
     * @param solution
     * @param webcon
     * @return
     */
    @RequestMapping("/exec")
    @ResponseBody
    @MustLogin
    @SocketSend(params = {"#solution"}, sendClass = StartSolutionTaskSend.class, isAfter = false)
    public ResultData<Integer> execSolut(Solution solution, @LoginUser Webcon webcon){
        solution.setAccount(webcon.getAccount());
        int result = solutionService.execSolution(solution);
        return new ResultData<Integer>().setData(result);
    }
}
